package com.shundr.common;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.http.CommonRequest;
import com.shundr.common.model.VersionInfo;
import com.shundr.common.util.ClearCacheUtil;
import com.shundr.common.util.DialogUtil;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.DialodUpdateApp;
import com.shundr.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.frame.constant.LConstants;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.util.GetVersionUtil;
import com.shundr.user.ChangePasswordActivity;
import com.shundr.user.ChangePhoneActivity;
import com.shundr.user.LoginActivity;

/**
 * 
 * SettingActivity 系统设置
 * 
 * @author larry 2015-5-16 下午4:55:31 
 * @version 1.0.0
 *
 */
public class SettingActivity extends BaseActivity implements OnClickListener{

	private Button btnClear;
	private Button btnFeedback;
	private Button btnCheckUpdate;
	private Button btnExit;
	private Button btnChangePhone;
	private Button btnChangePassword;
	
	@SuppressLint("HandlerLeak")
	Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				String description="";
				switch (msg.what) {
				case CommonRequest.GET_APPSYS_INFO_UPDATE:
					VersionInfo version = (VersionInfo)msg.obj;
					if(version == null)
						return;
					DialodUpdateApp dialodUpdateApp=new DialodUpdateApp(mContext, version);
					dialodUpdateApp.show();
					break;
				case CommonRequest.GET_APPSYS_INFO_NO_UPDATE:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				case CommonRequest.GET_APPSYS_INFO_FAILED:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				case CommonRequest.REQUEST_FAILED:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		btnClear=(Button)findViewById(R.id.btn_clear_cache);
		btnFeedback=(Button)findViewById(R.id.btn_feedback);
		btnCheckUpdate = (Button) findViewById(R.id.btn_about_update);
		btnExit = (Button) findViewById(R.id.btn_exit);
		btnChangePhone=(Button)findViewById(R.id.btn_change_phone);
		btnChangePassword=(Button)findViewById(R.id.btn_change_password);
		btnClear.setOnClickListener(this);
		btnCheckUpdate.setOnClickListener(this);
		btnFeedback.setOnClickListener(this);
		btnExit.setOnClickListener(this);
		btnChangePhone.setOnClickListener(this);
		btnChangePassword.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		mHandler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clear_cache:
			DialogUtil.noticeAlertDialog(mContext, "清空缓存", "确认要清空本地缓存图片和文件吗?",
					new OnDisplayListener() {

						@Override
						public void confirm() {
							ProgressDialogHelper.show(mContext, "清空缓存中...");
							ClearCacheUtil.clearCache(new File(LConstants.DOWNLOAD_APP_PATH));
							ClearCacheUtil.clearCache(new File(LConstants.TEMP_PATH));
							mHandler.postDelayed(new Runnable() {
								
								@Override
								public void run() {
									ProgressDialogHelper.dismiss();
									ToastHelper.toastShort(mContext, "清空成功");
								}
							}, 2000);
						}

						@Override
						public void cancel() {

						}
					});
			break;
		case R.id.btn_feedback:
			startActivity(new Intent(mContext, FeedbackActivity.class));
			break;
		case R.id.btn_change_phone:
			if(LContext.isLogin()){
				startActivity(new Intent(mContext, ChangePhoneActivity.class));
			}else{
				startActivity(new Intent(mContext, LoginActivity.class));
			}
			break;
		case R.id.btn_change_password:
			if(LContext.isLogin()){
				startActivity(new Intent(mContext, ChangePasswordActivity.class));
			}else{
				startActivity(new Intent(mContext, ChangePasswordActivity.class));
			}
			break;
		case R.id.btn_about_update:
			try {
				int VersionCode=GetVersionUtil.getVersionCode(getApplicationContext());
				int androidVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
				new CommonRequest(mContext, mHandler).getAppSysInfo(VersionCode+"", androidVersion+"");
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.btn_exit:
			DialogUtil.noticeAlertDialog(mContext, "退出应用", "确认退出应用吗?",
					new OnDisplayListener() {

						@Override
						public void confirm() {
							exitAPP();
						}

						@Override
						public void cancel() {

						}
					});
			break;
		default:
			break;
		}
	}
}
